import router from "@system.router";
import http from "@ohos.net.http";
import prompt from "@system.prompt";
import {isPasswordValid} from "../../../common/js/utils.js";

export default {
    data: {
        title: "World",
        password: "",
    },
    props: {
        // 从 code 页面传参
        phone: {
            default: "",
        },
    },
    onPasswordInput(ev) {
        this.password = ev.value;
    },
    confirm() {
        if (!isPasswordValid(this.password)) {
            prompt.showToast({
                message: "请输入6~20位数字字母组合密码",
            });
            return;
        }
        // 修改密码
        const req = http.createHttp();
        const promise = req.request("https://trezedo.club/api/ha/user", {
            method: "PUT",
            extraData: {
                newPassword: this.password,
                password: "123456",
                phone: this.phone,
            },
        });
        promise.then(({ result }) => {
            console.info("!! " + JSON.stringify(result));
            result = JSON.parse(result);
            // @ts-ignore
            if (result.code == 200) {
                // 注册转到空页面
                router.replace({
                    uri: "pages/home_nothing/home_nothing",
                });
            } else {
                prompt.showToast({
                    // @ts-ignore
                    message: result.msg
                });
            }
        });
    },
};
